package com.rtx.edi.platform.dash.error.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.edi.model.base.Constant;
import com.rtx.edi.model.error.po.ApiOrdersfinish;
import com.rtx.edi.platform.dash.error.mapper.PlApiOrdersfinishMapper;
import com.rtx.edi.platform.dash.error.service.PlApiOrdersfinishService;
import com.rtx.wms.std.common.bean.WmsConstant;

@Service
public class PlApiOrdersfinishServiceImpl extends ServiceImpl<PlApiOrdersfinishMapper,ApiOrdersfinish>  implements PlApiOrdersfinishService {
	
	@DataSourceAnnotation
	@Override
	public void updApiOrdersfinish(String whseid, ApiOrdersfinish apiOrdersfinish) {
		LambdaUpdateWrapper<ApiOrdersfinish> update =Wrappers.lambdaUpdate();
		update.eq( ApiOrdersfinish::getSerialkey, apiOrdersfinish.getSerialkey())
		.set(ApiOrdersfinish::getEdiflag, Constant.DEFAULT_FLAG)
		.set(ApiOrdersfinish::getRtxqueue,"0");
		baseMapper.update(null, update);
	}
	
	
	@DataSourceAnnotation
	@Override
	public void updApiOrdersfinishSuccess(String whseid, ApiOrdersfinish apiOrdersfinish) {
		LambdaUpdateWrapper<ApiOrdersfinish> update =Wrappers.lambdaUpdate();
		 update.eq(ApiOrdersfinish::getSerialkey,apiOrdersfinish.getSerialkey())
		 .set(ApiOrdersfinish::getEdiflag,Constant.SUCCESS_FLAG)
		 .set(ApiOrdersfinish::getDefault10,new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date())+"手动标记为成功")
		 .set(ApiOrdersfinish::getRtxqueue,0);
		baseMapper.update(null, update);
	}

	@DataSourceAnnotation
	@Override
	public void selectApiOrderFinishErrorList(String datasourcekey,Page<ApiOrdersfinish> pageParam, ApiOrdersfinish error) {
		LambdaQueryWrapper<ApiOrdersfinish> query =Wrappers.lambdaQuery();
		query.eq(ApiOrdersfinish::getEdiflag,WmsConstant.FAIL_FLAG).or().gt(ApiOrdersfinish::getRtxqueue, WmsConstant.DEFAULT_COUNT);
		
		if(error!=null) {
			query.eq( error.getExternorderkey()!=null,ApiOrdersfinish::getExternorderkey, error.getExternorderkey())
			.eq( error.getSerialkey()!=null,ApiOrdersfinish::getSerialkey, error.getSerialkey())
			.eq( error.getWhseid()!=null,ApiOrdersfinish::getWhseid, error.getWhseid()) ;
		}
		baseMapper.selectPage(pageParam,query);
	}
}
